﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QDT.Core.Projects;

namespace QDT.Services.Projects
{
    public class DefaultRepayStrategyFactory:IRepayStrategyFactory
    {
        #region Implementation of IRepayStrategyFactory

        /// <summary>
        /// 获取指定Project的还款策略
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public IRepayStrategy GetRepayStrategy(Project project) {
            
            if (project.Status != ProjectStatus.Repaying && project.Status != ProjectStatus.Repaid)
            {
                throw new Exception("Project未募资成功，无法计算还款计划.");
            }

            if(project.RepaymentID==Project.MONTH_INTEREST_PAYMENT) {
                //利率除100，因为在Project是乘100后的值
                return new MonthRepaymentStrategy(project.SuccessTime.Value,project.RepayTime,project.LoadMoney,project.Rate/100);
            }

            if(project.RepaymentID==Project.DAY_INTEREST_PAYMENT) {
                
                var start = project.SuccessTime.Value;

                var end = start.AddMonths(project.RepayTime);

                var settle = project.SettleDay;

                if(project.SettleDay<=0&&project.SettleDay>28) {
                    settle = 15;
                }

                return new DayRepaymentStrategy(start,end,settle,project.LoadMoney,project.Rate);
            }

            return null;

        }

        #endregion
    }
}
